﻿/*	
	stencil - action script 3 isometric game engine.

	http://code.google.com/p/stencilas3/
	
	Copyright (c) 2010 Max Zhuravkov, All Rights Reserved.

	Permission is hereby granted, free of charge, to any person obtaining a copy of
	this software and associated documentation files (the "Software"), to deal in
	the Software without restriction, including without limitation the rights to
	use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
	of the Software, and to permit persons to whom the Software is furnished to do
	so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	SOFTWARE.
*/
package stencil.core.engine 
{
	import flash.events.EventDispatcher;
	import stencil.core.engine.ISceneObject;
	import stencil.core.errors.NotImplementedMethodError;
	import stencil.core.engine.errors.IllegalStateError;
	
	/**
	 * An implementation of IComponent interface which provides all basic functionality.
	 * 
	 * @author Max Zhuravkov
	 */
	public class AbstractComponent extends EventDispatcher implements IComponent {
		
		private var _owner:ISceneObject;
		
		private var _enabled:Boolean = true;
		
		private var _disposed:Boolean;
		
		private var _data:Object;
		
		private var _id:String;
		
		//----------------------------------------------------------------
		// Constructor
		//----------------------------------------------------------------
		
		public function AbstractComponent() {
			
		}
		
		//----------------------------------------------------------------
		// Public properties
		//----------------------------------------------------------------
		
		/**
		 * @inheritDoc
		 */
		public function set id(value:String):void {
			if (this._id == value) {
				return;
			}
			this._id = value;
		}
		
		
		public function get id():String {
			return this._id;
		}
		
		/**
		 * @inheritDoc
		 */
		public final function set owner(value:ISceneObject):void {
			this.checkState();
			if (this._owner == value) {
				return;
			}
			if (this._owner) {
				this.ownerDetached(this._owner);
			}
			this._owner = value;
			if (this._owner) {
				this.ownerAttached(this._owner);
			}
		}
		
		public final function get owner():ISceneObject {
			return this._owner;
		}
		
		/**
		 * @inheritDoc
		 */
		public function set data(value:Object):void {
			this.checkState();
			if (this._data) {
				this.dataDetached(this._data);
			}
			this._data = value;
			if (this._data) {
				this.dataAttached(this._data);
			}
		}
		
		public function get data():Object {
			return this._data;
		}
		
		/**
		 * @inheritDoc
		 */
		public final function set enabled(value:Boolean):void {
			this.checkState();
			if (this._enabled == value) {
				return;
			}
			this._enabled = value;
			this.enabledChanged(value);
		}
		
		public final function get enabled():Boolean {
			return this._enabled;
		}
		
		//----------------------------------------------------------------
		// Public methods
		//----------------------------------------------------------------
		
		/**
		 * @inheritDoc
		 */
		public final function dispose():void {
			this.releaseResources();
			this._disposed = true;
		}
		
		//----------------------------------------------------------------
		// Protected methods
		//----------------------------------------------------------------
		
		protected final function checkState():void {
			if (this._disposed) {
				throw new IllegalStateError("Object '" + this + "' has been disposed.");
			}
			this.additionalStateChecks();
		}
		
		protected function additionalStateChecks():void {
			
		}
		
		/**
		 * Called when this component is attached to the given scene object.
		 * 
		 * @param	owner	the new owner of this component.
		 */
		protected function ownerAttached(owner:ISceneObject):void {
			
		}
		
		/**
		 * Called when this component is detached from the given scene object.
		 * 
		 * @param	owner		the previous owner of this component.
		 */
		protected function ownerDetached(owner:ISceneObject):void {
			
		}
		
		/**
		 * Called when the given data object is attached to this component.
		 * 
		 * @param	data	the data object.
		 */
		protected function dataAttached(data:Object):void {
			
		}
		
		/**
		 * Called when the given data object is detached from this component.
		 * 
		 * @param	data	the data object.
		 */
		protected function dataDetached(data:Object):void {
			
		}
		
		/**
		 * Called when this component is enabled or disabled.
		 * 
		 * @param	enabled		true if this component was enabled and false otherwise.
		 */
		protected function enabledChanged(enabled:Boolean):void {
			
		}
		
		/**
		 * Called when the componet is to be disposed. Releases resources acquired by this component.
		 */
		protected function releaseResources():void {
			
		}
		
	}

}